<script setup>
import {
  onBeforeUnmount,
  onMounted,
  reactive,
  toRefs,
  watch
} from 'vue';
import * as echarts from "echarts";
const props = defineProps({
  numTestData:{
    required: true
  }
})
const {numTestData} = toRefs(props);
let chart1
const option1 = reactive({
  title: {
    text: '使用多线程算法用时随文本量的变化'
  },
  tooltip: {
    trigger: 'axis'
  },
  legend: {
    data: ['Ansj', 'HanlP', 'apdplat word'],
    left: "right"
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
  },
  toolbox: {
    feature: {
      saveAsImage: {}
    }
  },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: ['4', '8', '12', '16', '20', '24', '28','32' ,'36', '40', '44']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      name: 'Ansj',
      type: 'line',
      stack: 'Total',
      data: [120, 132, 101, 134, 90, 230, 210]
    },
    {
      name: 'HanlP',
      type: 'line',
      stack: 'Total',
      data: [220, 182, 191, 234, 290, 330, 310]
    },
    {
      name: 'apdplat word',
      type: 'line',
      stack: 'Total',
      data: [150, 232, 201, 154, 190, 330, 410]
    }
  ]
})

let chart2
const option2 = reactive({
  title: {
    text: '未使用多线程算法用时随文本量的变化'
  },
  tooltip: {
    trigger: 'axis'
  },
  legend: {
    data: ['Ansj', 'HanlP'],
    left: "right"
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
  },
  toolbox: {
    feature: {
      saveAsImage: {}
    }
  },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: ['4', '8', '12', '16', '20', '24', '28','32' ,'36', '40', '44']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      name: 'Ansj',
      type: 'line',
      stack: 'Total',
      data: [120, 132, 101, 134, 90, 230, 210]
    },
    {
      name: 'HanlP',
      type: 'line',
      stack: 'Total',
      data: [220, 182, 191, 234, 290, 330, 310]
    }
  ]
})

onMounted(() => {
  initChart()
})
watch(() =>numTestData,(newValue) => {
  resetChart(newValue.value)
})
onBeforeUnmount(() => {
  if (chart1.value) {
    chart1.value.dispose();
  }
})
const initChart = () => {
  chart1 = echarts.init(document.getElementById('numTestChart1'));
  chart2 = echarts.init(document.getElementById('numTestChart2'));
  resetChart(numTestData.value)
}
const resetChart = (data) => {
  option1.series[0].data = data[0].dataList;
  option1.series[1].data = data[2].dataList;
  option1.series[2].data = data[4].dataList;

  option2.series[0].data = data[1].dataList;
  option2.series[1].data = data[3].dataList;

  chart1.setOption(option1)
  chart2.setOption(option2)
}
</script>

<template>
  <div class="panel-container">
    <div class="card-container">
      <div id="numTestChart1" style="width: 100%; height: 41vh;"></div>
      <div id="numTestChart2" style="width: 100%; height: 41vh;"></div>
    </div>
    <div class="button-div">
      <el-button type="primary" color="#EC5832" @click="()=>{chart1.resize();chart2.resize()}" plain>重置图像</el-button>
    </div>
  </div>

</template>

<style scoped>
.panel-container{
  width: 100%;
  height: 50vh;
  display: flex;
  flex-direction: column;
}
.card-container{
  width: 100%;
  height: 50vh;
  display: flex;
  justify-content: center;
}

.button-div{
  display: flex;
  flex-direction: column;
  justify-content: center;
  margin-top: 20px;
  margin-bottom: 20px;
}
</style>